package com.qf.smartdevice.service.impl;

import com.qf.smartdevice.cache.CategoryCache;
import com.qf.smartdevice.cache.SceneCache;
import com.qf.smartdevice.events.enums.CheckType;
import com.qf.smartdevice.exception.AddDataException;
import com.qf.smartdevice.exception.ErrorCode;
import com.qf.smartdevice.exception.QueryDataException;
import com.qf.smartdevice.mapper.SysDeviceMapper;
import com.qf.smartdevice.pojo.SysCategory;
import com.qf.smartdevice.pojo.SysDevice;
import com.qf.smartdevice.pojo.SysScene;
import com.qf.smartdevice.pojo.SysUserInfo;
import com.qf.smartdevice.service.SysDeviceService;
import com.qf.smartdevice.utils.LoginUserInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author 王博康
 * @version 1.0
 * @project devicemanagementsystem
 * @description: TODO
 * @date 2022/3/25 11:12 星期五
 */
@Service
@Transactional
public class SysDeviceServiceImpl implements SysDeviceService {

    private CategoryCache categoryCache;

    @Autowired
    public void setCategoryCache(CategoryCache categoryCache) {
        this.categoryCache = categoryCache;
    }

    private SceneCache sceneCache;

    @Autowired
    public void setSceneCache(SceneCache sceneCache) {
        this.sceneCache = sceneCache;
    }

    private SysDeviceMapper sysDeviceMapper;

    @Resource
    public void setSysDeviceMapper(SysDeviceMapper sysDeviceMapper) {
        this.sysDeviceMapper = sysDeviceMapper;
    }


    @Override
    public int addSysDevice(SysDevice sysDevice) {
        //判空
        Assert.isTrue(!sysDevice.isEmpty(CheckType.ADD),()->{
            throw new AddDataException("数据为空", ErrorCode.DATA_NULL);
        } );
        //判断是否登录
        SysUserInfo loginUser = LoginUserInfoUtil.getLoginUser();
        Assert.notNull(loginUser,()->{
            throw new AddDataException("未登录",ErrorCode.NOT_LOGIN );
        } );
        //登录后设置绑定用户
        sysDevice.setBindUserId(loginUser.getUId());
        //检查设备是否已经被绑定了
        SysDevice device=sysDeviceMapper.findById(sysDevice.getDeviceId());
        Assert.isNull(device,()->{
            throw new AddDataException("设备被绑定了", ErrorCode.DEVICE_ALREADY_BOUND);
        } );
        //检查场景是不是空的,如果是空的,设置默认值-1;如果不是空的,需检查场景是否属于该用户
        if (sysDevice.getSceneId()==null){
            sysDevice.setSceneId(-1L);
        }else if (sysDevice.getSceneId()!=-1){
            SysScene sysScene = sceneCache.get(sysDevice.getSceneId());
            //断言场景一定是存在的,且属于该用户
            Assert.isTrue(sysScene!=null&&sysScene.getCreateBy()==loginUser.getUId(),()->{
                throw new AddDataException("场景不存在",ErrorCode.SCENE_NONENTITY );
            } );
        }

        //检查分类是不是空的,分类不能为空,但可以是已删除状态
        SysCategory sysCategory = categoryCache.getWithDelete(sysDevice.getCategyId());
        Assert.notNull(sysCategory,()->{
            throw new AddDataException("分类不存在",ErrorCode.CATEGORY_NONENTITY );
        } );

        //设置绑定时间
        sysDevice.setBindTime(new Date());
        //操作数据库
        int count = sysDeviceMapper.addSysDevice(sysDevice);
        return count;
    }


    public SysDevice findById(String deviceId){
        //检查是否登录
        SysUserInfo loginUser = LoginUserInfoUtil.getLoginUser();
        Assert.notNull(loginUser,()->{
            throw new QueryDataException("未登录",ErrorCode.NOT_LOGIN);
        } );
        //检查设备是否输入登录用户
        SysDevice sysDevice = sysDeviceMapper.findById(deviceId);
        //断言设备存在,且是登录用户的
        Assert.isTrue(sysDevice!=null&&sysDevice.getBindUserId()==loginUser.getUId(),()->{
            throw new QueryDataException("设备不存在",ErrorCode.DEVICE_NONENTITY );
        } );
        return sysDevice;
    }

    @Override
    public void updateDeviceById(SysDevice sysDevice) {
        sysDeviceMapper.updateDeviceById(sysDevice);
    }
}
